package guda.house.web.action.sys;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.mvc.form.Form;
import guda.grape.util.RequestUtil;
import guda.house.biz.EstateBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.form.village.EstateEditForm;
import guda.house.biz.form.village.EstateForm;
import guda.house.biz.query.VillageQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dataobject.EstateDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created by well on 15/11/13.
 */
@Controller
public class EstateAction {

    @Autowired
    private EstateBiz estateBiz;

    @RequestMapping(value = "sys/estate/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        VillageQuery villageQuery = new VillageQuery();
        villageQuery.setPageNo(pageId);
        villageQuery.setPageSize(pageSize);
        modelMap.put("query", villageQuery);
        BizResult bizResult = estateBiz.list(villageQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/estate/list.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }

    @RequestMapping(value = "sys/estate/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, EstateEditForm estateEditForm,
                       BindingResult result) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = estateBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            estateEditForm.initForm(((EstateDO) bizResult.data.get("estateDO")));
            return "sys/estate/edit.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/estate/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = estateBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/estate/detail.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/estate/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, EstateForm estateForm,
                         BindingResult result) {
        return "sys/estate/create.vm";
    }

    @Form
    @RequestMapping(value = "sys/estate/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap, @Valid EstateForm estateForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            return "sys/estate/create.vm";
        }
        EstateDO estateDO = estateForm.toDO();
        BizResult bizResult = estateBiz.create(estateDO);
        if (bizResult.success) {
            return "redirect:/sys/estate/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/estate/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, @Valid EstateEditForm estateEditForm,
                           BindingResult result ) {
        if (result.hasErrors()) {
            return "sys/estate/edit.vm";
        }
        EstateDO estateDO = estateEditForm.toDO();
        BizResult bizResult = estateBiz.update(estateDO);
        if (bizResult.success) {
            return "redirect:/sys/estate/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/estate/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = estateBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
